if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false
	SWEP.HoldType			= "slam"

end

if ( CLIENT ) then

	SWEP.DrawAmmo			= false
	SWEP.DrawCrosshair		= false
	SWEP.ViewModelFOV		= 75
	SWEP.ViewModelFlip		= false
	SWEP.CSMuzzleFlashes	= false
	
	SWEP.IconFont = "CSSelectIcons"
	
	SWEP.PrintName = "Medikit"
	SWEP.Slot = 1
	SWEP.Slotpos = 1
	SWEP.IconLetter = "F"

end

SWEP.Base = "tp_base"

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true

SWEP.Primary.Recoil			= 1.25
SWEP.Primary.Damage			= 0
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.02
SWEP.Primary.Delay			= 1

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

SWEP.ViewModel = "models/weapons/v_c4.mdl"
SWEP.WorldModel = "models/weapons/w_c4.mdl" 

SWEP.Deny = Sound("SuitRecharge.Deny")
SWEP.Hp = Sound("HealthKit.Touch")

function SWEP:Initialize()
    if (SERVER) then
		self:SetWeaponHoldType(self.HoldType)
	end
end 

function SWEP:GetUsedAmmoType()
	return "none"
end

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
	return true
end  

function SWEP:DrawHUD()
	local w,h = ScrW(),ScrH()
	
	surface.SetDrawColor(0, 220, 0, 220)
	local wh, lh, sh = w*.5, h*.5, 10
	surface.DrawLine(wh - sh, lh - sh, wh + sh, lh - sh) //top line
	surface.DrawLine(wh - sh, lh + sh, wh + sh, lh + sh) //bottom line
	surface.DrawLine(wh - sh, lh - sh, wh - sh, lh + sh) //left line
	surface.DrawLine(wh + sh, lh - sh, wh + sh, lh + sh) //right line
end

function SWEP:Think()

end

function SWEP:CanPrimaryAttack()
	return true
end

function SWEP:PrimaryAttack()
	if CLIENT then return end

	if self.SecondaryNext > CurTime() then return end
	self.SecondaryNext = CurTime() + .8

	if not self.Owner.MediHint then
		self.Owner.MediHint = true
		Inventory.Notice(self.Owner, "Secondary fire heals your allies", Inventory.White)
	end

	if self.Owner:Health() == self.Owner.MaxHealth then
		self.Owner:EmitSound(self.Deny)
		Inventory.Notice(self.Owner, "You aren't injured", Inventory.Red)
	else
		local inv = self.Owner:GetItemInventory()
		local valids = {}
		for k,v in pairs(inv) do
			local tbl = Inventory.FindTable(v)
			local validfuncs = {Inventory.Eat,Inventory.Aid,Inventory.Drink,Inventory.DrinkB,Inventory.DrinkC}
			for c,d in pairs(validfuncs) do
				if table.HasValue(tbl.Functions,d) then
					table.insert(valids,tbl.Model)
				end
			end
		end
		if table.Count(valids) >= 2 then
				
			self.Owner:AddHealth(15)
			self.Owner:AddStamina(100)
			self.Owner:StopBleeding()
			self.Owner:StopBlindness()
			Inventory.Notice(self.Owner, "Gained 15 HP", Inventory.Blue)
			Inventory.Notice(self.Owner, "Stopped all bleeding", Inventory.Blue)
			Inventory.Notice(self.Owner, "Restored stamina", Inventory.Blue)
			Inventory.Notice(self.Owner, "Medikit used up 2 items", Inventory.Red)
					
			self.Owner:EmitSound(self.Hp)
					
			self.Owner:RemoveFromInventory(valids[1])
			self.Owner:RemoveFromInventory(valids[2])
		else
			Inventory.Notice(self.Owner, "You need more items", Inventory.Red)
			self.Owner:EmitSound(self.Deny)
		end
	end
end
 
SWEP.SecondaryNext = 0
function SWEP:SecondaryAttack()
	if CLIENT then return end

	if self.SecondaryNext > CurTime() then return end
	self.SecondaryNext = CurTime() + .8

	local checkpos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * 70
	local len = 100
	local curplayer
	for k,v in pairs(team.GetPlayers(TEAM_HUMANS)) do
		if v:GetPos():Distance(checkpos) < len and v != self.Owner then
			len = v:GetPos():Distance(checkpos)
			curplayer = v
		end
	end
	if curplayer then
		if curplayer:Health() == curplayer.MaxHealth then
			self.Owner:EmitSound(self.Deny)
			Inventory.Notice(self.Owner, "That person isn't injured", Inventory.Red)
		else
			if not self.Owner.PlayerTable then self.Owner.PlayerTable = {} end
			if table.HasValue(self.Owner.PlayerTable,curplayer) then
				self.Owner:EmitSound(self.Deny)
				Inventory.Notice(self.Owner, "You already healed that person", Inventory.Red)
				self.Weapon:SetNextPrimaryFire( CurTime() + .5 )
			else
				table.insert(self.Owner.PlayerTable,curplayer)
				curplayer:AddHealth(10)
				curplayer:AddStamina(50)
				curplayer:StopBleeding()
				curplayer:StopBlindness()
				Inventory.Notice(curplayer, "Gained 10 HP", Inventory.Blue)
				Inventory.Notice(curplayer, "Stopped all bleeding", Inventory.Blue)
				Inventory.Notice(curplayer, "Restored some stamina", Inventory.Blue)
				Inventory.Notice(self.Owner, "You healed "..curplayer:Name(), Inventory.Green)
				self.Owner:AddAwardPoints(AWARD_GOOD_MEDIC,1)
				self.Owner:VoiceSound(ChooseRandom(self.Owner.Heal),100,.1)
				self.Owner:EmitSound(Sound(ChooseRandom(GameSounds.HealthVial)),100,math.random(90,110))
				if table.Count(self.Owner.PlayerTable) >= math.Clamp(team.NumPlayers(TEAM_HUMANS) - 1,1,2) then
					self.Owner.PlayerTable = {}
					Inventory.Notice(self.Owner, "You healed "..math.Clamp(team.NumPlayers(TEAM_HUMANS) - 1,1,2).." allies", Inventory.Green)
					self.Owner:AddPoints(math.Clamp(team.NumPlayers(TEAM_HUMANS),1,3))
				end
			end
		end
	else
		Inventory.Notice(self.Owner, "You're too far away", Inventory.Red)
		self.Owner:EmitSound(self.Deny)
	end
end
